﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CustomORM.Extensions
{
    public static class SqlQueryExtensions
    {
        public static string ToCommaSeparatedString(this IEnumerable<int> list)
        {
            if (list != null && list.Count() > 0)
            {
                var result = new StringBuilder();
                const string delimeter = ",";
                foreach (int item in list)
                {
                    result.Append(item);
                    result.Append(delimeter);
                }
                result.Remove(result.Length - 1, 1);
                return result.ToString();
            }
            else
            {
                throw new ArgumentException("list should not be empty");
            }
        }

        public static string ToCommaSeparatedString(this IEnumerable list, string prefix = null, string syffix = null, int elementsToSkip = 0, bool eatSpaces = false)
        {
            var enumerator = list.GetEnumerator();
            enumerator.MoveNext();
            if (list != null && enumerator.Current != null)
            {
                var result = new StringBuilder();
                const string delimeter = ",";
                foreach (object item in list)
                {
                    if (elementsToSkip <= 0)
                    {
                        var itemString = item.ToString();
                        if(eatSpaces)
                        {
                            itemString = itemString.Replace(" ", "");    
                        }
                        result.AppendFormat("{0}{1}{2}{3}", prefix, itemString, syffix, delimeter);    
                    }
                    --elementsToSkip;
                }
                result.Remove(result.Length - 1, 1);
                return result.ToString();
            }
            else
            {
                throw new ArgumentException("list should not be empty");
            }
        }
    }
}
